from sqlalchemy import Column, Integer
from sqlalchemy import Float
from SqlClass import DbSession, Base
from SqlClass.digiccy_contract_strategy import digiccy_contract_strategy
from SqlClass.digiccy_contract_symbol import digiccy_contract_symbol
from SqlClass.digiccy_member import digiccy_member


class digiccy_member_contract_activation(Base):
    __tablename__ = 'digiccy_member_contract_activation'  # 表名
    id = Column(Integer, primary_key=True)  # id:int类型,是主键

    member_id = Column(Integer)
    digiccy_member = None

    symbol_id = Column(Integer)
    digiccy_contract_symbol = None

    strategy_id = Column(Integer)
    digiccy_contract_strategy = None

    status = Column(Integer)
    origin_assessment = Column(Float)
    outstand_assessment = Column(Float)

    def get_digiccy_member(self, update=False):
        session = DbSession()
        if self.digiccy_member is None or update:
            self.digiccy_member = session.query(digiccy_member).filter_by(id=self.member_id).first()

        session.close()
        return self.digiccy_member

    def get_digiccy_contract_symbol(self, update=False):
        session = DbSession()
        if self.digiccy_contract_symbol is None or update:
            self.digiccy_contract_symbol = session.query(digiccy_contract_symbol).filter_by(id=self.symbol_id).first()

        session.close()
        return self.digiccy_contract_symbol

    def get_digiccy_contract_strategy(self, update=False):
        session = DbSession()

        if self.digiccy_contract_strategy is None or update:
            self.digiccy_contract_strategy = session.query(digiccy_contract_strategy).filter_by(
                id=self.strategy_id).first()

        session.close()
        return self.digiccy_contract_strategy

    def stop(self):
        session = DbSession()
        session.query(self).filter_by(id=self.id).update("status", 0)
        session.commit()
        session.close()